<?php
namespace Admin\Controller;
// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
use Admin\Controller\AdminController;
use Libs\Logs;
header("content-type:text/html;charset=utf-8");

class BrandController extends AdminController{
    
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'goods'); // 数据集
    } 
    
    //品牌列表
    public function index(){
        
        $title = I('title');
        if(!empty($title)){
            $where['title'] = $title;
        }
        
        $Brand = M("Brand");
        $count = $Brand->where($where)->count();
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        
        $show = $Page->show(); // 分页显示输出
        $arr = $Brand->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        foreach ($arr as $key => $value) {
            $arr[$key]['cat_name'] = M('category')->where('cat_id='.$value['cat_id'])->getField('cat_name');
        }
        
        $this->assign('pages',$show);
        $this->assign('arr',$arr);
        $this->assign('title',$title);
        $this->assign('action', 'brand');
        $this->display();
    }
    public function add(){
        if(IS_POST){
            $data['cat_id'] = I('cat_id');
            $data['title'] = I('title');
            $data['letter'] = I('letter');
            $data['img'] = I('img');
            $data['state'] = I('state');
            $data['px'] = I('px');
            $re = M('Brand')->add($data);
            if($re){
                $this->success('品牌添加成功！',U('index'));
            }else{
                $this->error('品牌添加失败！');
            }
        }
        $map['parent_id'] = 0;
        $category = M('category')->where($map)->select();
        $this->assign('category',$category);
        
        $this->assign('action', 'brand');
        $this->display();
    }
    
    
    public function edit(){
        $id = I('id');
        $map['id'] = $id;
        if(IS_POST){
            $data['cat_id'] = I('cat_id');
            $data['title'] = I('title');
            $data['letter'] = I('letter');
            $data['img'] = I('img');
            $data['state'] = I('state');
            $data['px'] = I('px');
            $re = M('Brand')->where($map)->save($data);
            if($re){
                $this->success('品牌修改成功！',U('index'));
            }else{
                $this->error('品牌修改失败！');
            }
        }
        $map['parent_id'] = 0;
        $category = M('category')->where($map)->select();
        $this->assign('category',$category);
        $rs = M('Brand')->where($map)->find();
        $this->assign('rs',$rs);
        $this->assign('action', 'brand');
        $this->display();
    }
    
    public function del(){
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('品牌不能为空！');
        }
        if (M('Brand')->where('id=' . $Id)->delete()) {
            $this->success('删除成功！', U('index'));
        } else {
            $this->error('删除失败！');
        }
    }
    public function sonindex(){
        
        $id = I('id');
        $where['pid'] = $id;
        $Brand = M("Brand");
        $count = $Brand->where($where)->count();
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        
        $show = $Page->show(); // 分页显示输出
        $arr = $Brand->where($where)->order('px desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $state=array('隐藏','显示');
        $this->assign('pages',$show);
        $this->assign('arr',$arr);
        $this->assign('state',$state);
        $this->assign('title',$title);
        $this->display();
    }
    public function sonadd(){
        if(IS_POST){
            $data['title'] = I('title');            
            $data['pid'] = I('pid');
            $data['state'] = I('is_state');
            $data['px'] = I('px');
            $re = M('Brand')->add($data);
            if($re){
                $this->success('品牌添加成功！',U('sonindex',array('id'=>$data['pid'])));
            }else{
                $this->error('品牌添加失败！');
            }
        }
        $this->assign('pid',I('pid'));
        $this->display();
    }
    
    
    public function sonedit(){
        $id = I('id');
        $map['id'] = $id;
        if(IS_POST){
            $data['title'] = I('title');
            $data['state'] = I('is_state');
            $data['content'] = I('content');
            $data['px'] = I('px');
            $re = M('Brand')->where($map)->save($data);
            if($re){
                $this->success('品牌修改成功！',U('sonindex',array('id'=>I('pid'))));
            }else{
                $this->error('品牌修改失败！');
            }
        }
        $rs = M('Brand')->where($map)->find();
        $this->assign('rs',$rs);
        $this->display();
    }
    
    public function sondel(){
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('品牌不能为空！');
        }
        $pid=M('Brand')->where('id=' . $Id)->getField('pid');
        if (M('Brand')->where('id=' . $Id)->delete()) {
            $this->success('删除成功！', U('sonindex',array('id'=>$pid)));
        } else {
            $this->error('删除失败！');
        }
    }
}
